<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="route.query" :inline="true">
        <el-form-item label="工厂号：">
          <span>{{ route.query.plantCode }}</span>
        </el-form-item>
        <el-form-item label="年度：">
          <span>{{ route.query.workCalendarYear }}</span>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />

        <el-table-column
          label="日历安排名称"
          prop="freeDayName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="起始日期"
          prop="beginDate"
          min-width="100"
          show-overflow-tooltip
          header-align="center"
          align="center"
          :formatter="formatDate"
        />
        <el-table-column
          label="截止日期"
          prop="endDate"
          min-width="100"
          show-overflow-tooltip
          header-align="center"
          align="center"
          :formatter="formatDate"
        />
        <el-table-column label="类型" prop="freeDayType" min-width="100">
          <template #default="scope">
            {{ useDictionary("FreeDayType", scope.row.freeDayType) }}
          </template>
        </el-table-column>
        <el-table-column
          label="是否允许加班"
          prop="allowOvertime"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.allowOvertime === 1"
              >是</el-tag
            >
            <el-tag type="info" v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="创建人"
          prop="createdUserName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="修改时间"
          prop="lastUpdateTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="修改人"
          prop="lastUpdatedUserName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column fixed="right" label="操作" width="240" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="handleTableDelete(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <mdmfreeday-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { useRoute } from "vue-router";
const route = useRoute();

defineOptions({
  name: "MDMMDMFREEDAY",
  inheritAttrs: false,
});

console.log("路由传参", route.query);
// 请求参数
const requestApi = "/mdm/mdmfreeday/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  freeDayName: "",
  beginDateSearch: [],
  beginDateStart: "",
  beginDateEnd: "",
  endDateSearch: [],
  endDateStart: "",
  endDateEnd: "",
  freeDayType: "",
  allowOvertime: "",
  workCalendarId: route.query.id,
};

const deleteParam = {
  api: "/mdm/mdmfreeday/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mdm/mdmfreeday/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开休息日设定表单弹窗 */
function openDialog(roleObj?: Object) {
  addDialog.data = route.query.id;
  if (roleObj) {
    addDialog.title = "修改休息日设定";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增休息日设定";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开休息日设定表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "休息日设定详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getbeginDate(data: any) {
  if (data) {
    searchParam.value.beginDateStart = data[0];
    searchParam.value.beginDateEnd = data[1];
  } else {
    searchParam.value.beginDateStart = "";
    searchParam.value.beginDateEnd = "";
  }
}
function getendDate(data: any) {
  if (data) {
    searchParam.value.endDateStart = data[0];
    searchParam.value.endDateEnd = data[1];
  } else {
    searchParam.value.endDateStart = "";
    searchParam.value.endDateEnd = "";
  }
}
function formatDate(row, column) {
  const date = row[column.property];
  if (!date) return "";
  return date.split(" ")[0]; // 返回日期的年月日部分，而隐藏时间部分
}
</script>
